<?php


namespace App\Utility;


use EasySwoole\Component\Singleton;
use EasySwoole\RedisPool\Redis;

class RedisUtility
{
    use Singleton;

    public static $redisName = 'redis';

    /**
     * @param $key
     * @param $start
     * @param $stop
     * @param bool $withScores
     * @return mixed|null
     */
    public function zRange($key, $start, $stop, $withScores = false){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $start, $stop, $withScores){
            return $redis->zRange($key, $start, $stop, $withScores);
        },0);
    }

    /**
     * @param $key
     * @param $member
     * @return mixed|null
     */
    public function zScore($key, $member){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $member){
            return $redis->zScore($key, $member);
        },0);
    }

    /**
     * @param $key
     * @param $score1
     * @param $member1
     * @param mixed ...$data
     * @return mixed|null
     */
    public function zAdd($key, $score1, $member1, ...$data){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $score1, $member1,$data){
            return $redis->zAdd($key, $score1, $member1,$data);
        },0);
    }

    /**
     * @param $key
     * @param $value
     * @return mixed|null
     */
    public function push($key,$value)
    {
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$value){
            //判断是否数组
            if(is_array($value))
            {
                $value = json_encode($value,JSON_UNESCAPED_UNICODE);
            }
            return $redis->rPush($key,$value);
        },0);
    }

    /**
     * @param $key
     * @return mixed|null
     */
    public function pop($key)
    {
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->lPop($key);
        },0);
    }

    /**
     * @param $key
     * @return mixed|null
     */
    public function get($key){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->get($key);
        },0);
    }

    /**
     * @param $key
     * @param $expire过期时间，默认300秒也就是5分钟
     * @return mixed|null
     */
    public function set($key,$val,$expire=300){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$val,$expire){
            return $redis->set($key,$val,$expire);
        },0);
    }

    /**
     * @param $key
     * @return mixed|null
     */
    public function del($key){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->del($key);
        },0);
    }

    public function sAdd($key, $data){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$data){
            return $redis->sAdd($key, ...$data);
        },0);
    }

    public function sCard($key){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->sCard($key);
        },0);
    }

    public function sMembers($key){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->sMembers($key);
        },0);
    }

    /**
     * @param $key
     * @param $field
     * @param $data
     * @return mixed|null
     */
    public function hSet($key,$field,$data){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$field,$data){
            return $redis->hSet($key,$field,$data);
        },0);
    }

    /**
     * @param $key
     * @param $field
     * @return mixed|null
     */
    public function hGet($key,$field){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$field){
            return $redis->hGet($key,$field);
        },0);
    }

    /**
     * @param $key
     * @return mixed|null
     */
    public function hIncrBy($key, $field, $increment){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $field, $increment){
            return $redis->hIncrBy($key, $field, $increment);
        },0);
    }

    /**
     * @param $key
     * @return mixed|null
     */
    public function hGetAll($key){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key){
            return $redis->hGetAll($key);
        },0);
    }

    /**删除哈希表值
     * @param $key
     * @param $field
     * @return mixed|null
     */
    public function hDel($key,$field){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key,$field){
            return $redis->hDel($key,$field);
        },0);
    }


    public function setBit($key, $offset, $value){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $offset, $value){
            return $redis->setBit($key, $offset, $value);
        },0);
    }

    public function getBit($key, $offset){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $offset){
            return $redis->getBit($key, $offset);
        },0);
    }

    /**获取全部数量
     * @param $key
     * @param $offset
     * @return mixed|null
     */
    public function bigCount($key,$offset){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $offset){
            return $redis->rawCommand(['bitcount',$offset]);
        },0);
    }

    public function bitcount($key, $offset){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($key, $offset){
            return $redis->bitcount($key, $offset);
        },0);
    }

    /**
     * @param $cursor
     * @param null $pattern
     * @param $count
     * @return mixed|null
     */
    public function scan($cursor, $pattern = null, $count ){
        return Redis::invoke(self::$redisName,function (\EasySwoole\Redis\Redis $redis) use($cursor, $pattern, $count ){
            return $redis->scan($cursor, $pattern, $count);
        },0);
    }

    public function reset()
    {

    }
}